<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\AdminTenant\Action\Admin\Traits;

use Hyperf\Di\Annotation\Inject;
use MoChat\App\Tenant\Contract\TenantContract;
use MoChat\Plugin\AdminUser\Contract\AdminUserContract;
use MoChat\Plugin\AdminUser\Contract\AdminUserLevelContract;
use MoChat\Plugin\AdminWallet\Contract\AdminWalletContract;
use MoChat\Plugin\TenantOrder\Contract\TenantOrderContract;
use MoChat\Plugin\TenantProduct\Contract\TenantProductContract;
use MoChat\Plugin\TenantProduct\Contract\TenantProductSkuContract;
use MoChat\Plugin\TenantShop\Contract\TenantShopContract;

trait OrderTrait
{
    /**
     * @Inject
     * @var TenantContract
     */
    protected $tenantService;

    /**
     * @Inject
     * @var AdminUserContract
     */
    protected $adminUserService;

    /**
     * @Inject
     * @var TenantProductSkuContract
     */
    protected $tenantProductSkuService;

    /**
     * @Inject
     * @var AdminUserLevelContract
     */
    protected $adminUserLevelService;

    /**
     * @Inject
     * @var TenantProductContract
     */
    protected $tenantProductService;

    /**
     * @Inject
     * @var TenantShopContract
     */
    protected $tenantShopService;

    /**
     * @Inject
     * @var TenantOrderContract
     */
    protected $tenantOrderService;

    /**
     * @Inject
     * @var AdminWalletContract
     */
    protected $adminWalletService;

    private function order(int $skuId, array $order, int $tenantId, array $adminUser)
    {
        # # 查询商品id
        $sku = $this->tenantProductSkuService->getTenantProductSkuById($skuId, ['spu_id']);
        # # 查询店铺id
        $spu = $this->tenantProductService->getTenantProductById($sku['spuId'], ['shop_id']);
        # # 查询店铺名称
        $shop = $this->tenantShopService->getTenantShopById($spu['shopId'], ['name']);
        $orderData = [
            'user_id' => $adminUser['id'],
            'tenant_id' => $tenantId,
            'order_no' => 'P' . date('YmdHis', time()) . random_int(100000, 999999),
            'shop_id' => $spu['shopId'],
            'shop_name' => $shop['name'],
            'item_total' => $order['itemTotal'],
            'adjustment_total' => $order['adjustmentTotal'],
            'total' => $order['total'],
            'status' => 'fulfilled',
            'payment_status' => 'paid',
            'shipment_status' => 'shipped',
            'paid_at' => date('Y-m-d H:i:s'),
            'confirmed_at' => date('Y-m-d H:i:s'),
            'fulfilled_at' => date('Y-m-d H:i:s'),
            'created_at' => date('Y-m-d H:i:s'),
        ];
        # # 1、创建订单
        $orderId = $this->tenantOrderService->createTenantOrder($orderData);
        # # 2、创建订单商品
        $orderItemData = [
            'order_id' => $orderId,
            'sku_id' => $skuId,
            'product_id' => $sku['spuId'],
            'quantity' => 1,
            'unit_total' => $order['itemTotal'],
            'adjustment_total' => $order['adjustmentTotal'],
            'total' => $order['total'],
            'unit_price' => $order['itemTotal'],
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $orderItemId = $this->tenantOrderService->createTenantOrderItem($orderItemData);
        # # 3、创建订单运输
        $orderShipmentData = [
            'order_id' => $orderId,
            'method_id' => 0,
            'amount' => $order['total'],
            'status' => 'shipped',
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $orderShipmentId = $this->tenantOrderService->createTenantOrderShipment($orderShipmentData);
        # # 4、创建订单商品实体
        $orderItemUnitData = [
            'item_id' => $orderItemId,
            'shipment_id' => $orderShipmentId,
            'adjustment_total' => $order['adjustmentTotal'],
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $orderItemUnitId = $this->tenantOrderService->createTenantOrderItemUnit($orderItemUnitData);
        # # 5、创建订单调整记录
        $orderAdjustmentData = [
            'order_id' => $orderId,
            'order_item_id' => $orderItemId,
            'order_item_unit_id' => $orderItemUnitId,
            'type' => 'promotion',
            'label' => '代理优惠',
            'included' => 1,
            'amount' => $order['adjustmentTotal'],
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $orderAdjustmentId = $this->tenantOrderService->createTenantOrderAdjustment($orderAdjustmentData);
        # # 6、创建订单支付记录
        $orderPaymentData = [
            'order_id' => $orderId,
            'method_id' => 5,
            'amount' => $order['total'],
            'status' => 'paid',
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $orderPaymentId = $this->tenantOrderService->createTenantOrderPayment($orderPaymentData);
        # # 商品扣除库存
        $this->tenantProductSkuService->updateTenantProductSkuById($skuId, ['stock' => $order['stock'] - 1]);
        # # 代理金币处理
        $this->wallet($order, $adminUser);
    }

    private function wallet(array $order, array $adminUser)
    {
        # # 代理金币余额减少
        $balance = $order['walletBalance'] - ($order['total'] / 100);
        $this->adminUserService->updateAdminUserById($order['userId'], ['wallet_balance' => $balance]);
        # # 创建管理员钱包流水记录
        $walletData = [
            'user_id' => $order['userId'],
            'type' => 2,
            'amount' => $order['total'] / 100,
            'operator_id' => $adminUser['id'],
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $this->adminWalletService->createAdminWallet($walletData);
    }
}
